package com.ruoyi.tb.lmt.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.tb.lmt.domain.TEquipmentLog;
import com.ruoyi.tb.lmt.domain.vo.TbDistVo;
import com.ruoyi.tb.lmt.service.TEquipmentLogService;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tb.lmt.domain.vo.TEquipmentVo;
import com.ruoyi.tb.lmt.domain.bo.TEquipmentBo;
import com.ruoyi.tb.lmt.service.ITEquipmentService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 高价互换件管理
 *
 * @author ruoyi
 * @date 2024-01-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xha-lmt/tEquipment")
public class TEquipmentController extends BaseController {

    private final ITEquipmentService iTEquipmentService;

    @Autowired
    private TEquipmentLogService tEquipmentLogService;

    /**
     * 查询高价互换件管理列表
     */
    //@SaCheckPermission("xha-lmt:tEquipment:list")
    @GetMapping("/list")
    public TableDataInfo<TEquipmentVo> list(TEquipmentBo bo, PageQuery pageQuery) {
        return iTEquipmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出高价互换件管理列表
     */
    @SaCheckPermission("xha-lmt:tEquipment:export")
    @Log(title = "高价互换件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(String equipmentTypecode, String lmtNumber, String lmtLength, String stockStation, String status, String dateType, String repairStartDate, String repairEndDate,String equipmentCode,HttpServletResponse response) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        Date endDate = null;
        if (repairStartDate != null || repairEndDate != null) {
            startDate = formatter.parse(repairStartDate);
            endDate = formatter.parse(repairEndDate);
        }

        List<TEquipmentVo> list = iTEquipmentService.selectEquipment(equipmentTypecode, lmtNumber, lmtLength, stockStation, status, dateType, startDate, endDate,equipmentCode);
        ExcelUtil.exportExcel(list, "高价互换件管理", TEquipmentVo.class, response);
    }

    /**
     * 获取高价互换件管理详细信息
     *
     * @param ID 主键
     */
    @GetMapping("/{ID}")
    public R<TEquipmentVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable Long ID) {
        return R.ok(iTEquipmentService.queryById(ID));
    }
    @GetMapping("/getByCode/{code}")
    public R<TEquipmentVo> getInfo(@NotNull(message = "主键不能为空")
                                   @PathVariable String code) {
        return R.ok(iTEquipmentService.queryByCode(code));
    }

    /**
     * 新增高价互换件管理
     */
    @SaCheckPermission("xha-lmt:tEquipment:add")
    @Log(title = "高价互换件管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TEquipmentBo bo) {
        bo.setStatus("库存");
        return toAjax(iTEquipmentService.insertByBo(bo));
    }

    /**
     * 修改高价互换件管理
     */
    @SaCheckPermission("xha-lmt:tEquipment:edit")
    @Log(title = "高价互换件管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TEquipmentBo bo) {
        return toAjax(iTEquipmentService.updateByBo(bo));
    }

    /**
     * 删除高价互换件管理
     *
     * @param IDs 主键串
     */
    @SaCheckPermission("xha-lmt:tEquipment:remove")
    @Log(title = "高价互换件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] IDs) {
        return toAjax(iTEquipmentService.deleteWithValidByIds(Arrays.asList(IDs), true));
    }


    //查询高价互换件
    @GetMapping("/selectEquipment")
    public TableDataInfo<TEquipmentVo> selectEquipment(String equipmentTypecode, String lmtNumber, String lmtLength, String stockStation, String status, String dateType, String repairStartDate, String repairEndDate,
                                                       String equipmentCode,
                                                       PageQuery pageQuery) throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        Date endDate = null;
        if (repairStartDate != null || repairEndDate != null) {
            startDate = formatter.parse(repairStartDate);
            endDate = formatter.parse(repairEndDate);
        }
        return iTEquipmentService.selectEquipment(equipmentTypecode, lmtNumber, lmtLength, stockStation, status, dateType, startDate, endDate, equipmentCode, pageQuery);
    }

    //查询高价互换件
    //@SaCheckPermission("xha-lmt:tEquipment:selectDist")
    @GetMapping("/selectDist")
    public List<TbDistVo> selectEquipment(String equipmentTypecode) {
        return iTEquipmentService.selectByEquipment(equipmentTypecode);
    }

    //根据车号查询高价互换件
    //@SaCheckPermission("xha-lmt:tEquipment:EquByLmt")
    @GetMapping("/selectEquByLmt")
    public TableDataInfo<TEquipmentVo> selectEquByLmtNumber(String lmtNumber, PageQuery pageQuery) {
        return iTEquipmentService.selectEquByLmtNumber(lmtNumber, pageQuery);
    }

    /**
     * 点击报废
     *
     * @param id
     * @return
     */
    @PutMapping("/scrap/{id}")
    public R<Void> scrap(@PathVariable Long id) {
        TEquipmentBo bo = new TEquipmentBo();
        bo.setID(id);
        bo.setStatus("报废");
        //添加报废日志
        TEquipmentLog tEquipmentLog = new TEquipmentLog();
        tEquipmentLog.setEquipmentId(bo.getID());
        tEquipmentLog.setOprtType("报废");
        tEquipmentLog.setOprtDate(DateUtils.getNowDate());
        tEquipmentLogService.insertEquipmentLog(tEquipmentLog);
        return toAjax(iTEquipmentService.updateByBo(bo));
    }

    /**
     * 点击无法修复
     *
     * @param id
     * @return
     */
    @PutMapping("/beyond/repair/{id}")
    public R<Void> beyondRepair(@PathVariable Long id) {
        TEquipmentBo bo = new TEquipmentBo();
        bo.setID(id);
        bo.setStatus("无法修复");
        //添加无法修复日志
        TEquipmentLog tEquipmentLog = new TEquipmentLog();
        tEquipmentLog.setEquipmentId(bo.getID());
        tEquipmentLog.setOprtType("无法修复");
        tEquipmentLog.setOprtDate(DateUtils.getNowDate());
        tEquipmentLogService.insertEquipmentLog(tEquipmentLog);
        return toAjax(iTEquipmentService.updateByBo(bo));
    }

    /**
     * 点击入库
     *
     * @param id
     * @return
     */
    @PutMapping("/store/{id}")
    public R<Void> store(@PathVariable Long id) {
        TEquipmentBo bo = new TEquipmentBo();
        bo.setID(id);
        bo.setStatus("库存");
        //添加入库日志
        TEquipmentLog tEquipmentLog = new TEquipmentLog();
        tEquipmentLog.setEquipmentId(bo.getID());
        tEquipmentLog.setOprtType("入库");
        tEquipmentLog.setOprtDate(DateUtils.getNowDate());
        tEquipmentLogService.insertEquipmentLog(tEquipmentLog);
        return toAjax(iTEquipmentService.updateByBo(bo));
    }

    /**
     * 点击送修
     *
     * @param id
     * @return
     */
    @PutMapping("/send/repair/{id}")
    public R<Void> sendRepair(@PathVariable Long id) {
        TEquipmentBo bo = new TEquipmentBo();
        bo.setID(id);
        bo.setStatus("送修中");
        //添加入库日志
        TEquipmentLog tEquipmentLog = new TEquipmentLog();
        tEquipmentLog.setEquipmentId(bo.getID());
        tEquipmentLog.setOprtType("送修");
        tEquipmentLog.setOprtDate(DateUtils.getNowDate());
        tEquipmentLogService.insertEquipmentLog(tEquipmentLog);
        return toAjax(iTEquipmentService.updateByBo(bo));
    }
}
